using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ASClass;
/*
using System.Management;
using System.Runtime.InteropServices; 
*/
//calling up api functions.
//http://www.codeproject.com/csharp/c__and_api.asp
//http://groups.google.pl/group/microsoft.public.windows.developer.winfx.sdk/browse_thread/thread/f7dfd86d8176c037/62d8ae576fe18544%2362d8ae576fe18544

using System.Runtime.InteropServices;
//[DllImport("User32.dll")];
//[DllImport("user32.dll")] kernel


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern bool ExitWindowsEx(UInt32 uFlags, UInt32 dwReserved);
        // static extern bool ExitWindowsEx(UINT uFlags, DWORD dwReserved );
        DateTime ShutDownTime;
        TimeSpan WarningTime = new TimeSpan(0, 0, 1, 0);
        TimeSpan ToShutDown;
        int[] TimeTable ={ 1, 2, 5, 10, 15, 30, 60, 120 };

        public Form1()
        {
            InitializeComponent();
            comboBoxChooseTime.Items.Clear();
            string Tekst;
            for (int i = 0; i < TimeTable.Length; i++)
            {
                Tekst = KlasaPrzetwarzaniaStringow.MinutesToText(TimeTable[i]);
                comboBoxChooseTime.Items.Add(Tekst);
            }
            ResetTimer();
            LabelLast.Left = 4;
            LabelLast.Width = Width - 8;
        }

        public void SetTimer(TimeSpan ToShutDown)
        {
            DateTime CurrentTime = DateTime.Now;
            ShutDownTime = CurrentTime + ToShutDown;
            timer1.Enabled = true;
            buttonCancel.Visible = true;
            FormatLabelLast();
        }

        public void ResetTimer()
        {
            timer1.Enabled = false;
            buttonCancel.Visible = false;
            FormatLabelLast();
        }

        public void FormatLabelLast()
        {
            if (timer1.Enabled)
            {
                LabelLast.BackColor = this.BackColor;
            }
            else
            {
                LabelLast.BackColor = this.BackColor;
                LabelLast.Text = "--:--";
            }

        }


        public void ShutDown()
        {
            /*
            int EWX_FORCE = 4;
            int EWX_POWEROFF = 8;
            int EWX_REBOOT = 2;
            int EWX_SHUTDOWN = 1;
            int EWX_LOGOFF = 0;
            */
            bool OK = ExitWindowsEx(4 + 1, 0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime CurrentTime = DateTime.Now;
            ToShutDown = ShutDownTime.Subtract(CurrentTime);
            String TextHr = ToShutDown.Hours.ToString();
            String TextMi = ToShutDown.Minutes.ToString();
            String TextSe = ToShutDown.Seconds.ToString();
            if (ToShutDown < TimeSpan.Zero)
            {
                //label1.Text = "wylaczono";
                ShutDown();
            }
            LabelLast.Text = KlasaPrzetwarzaniaStringow.TimeSpanToString2(ToShutDown);
            if (TimeSpan.Compare(ToShutDown, WarningTime) < 0)
            {
                timerWarning.Enabled = true;
                if (TopMost == false)
                    TopMost = true;
            }
            else
            {
                timerWarning.Enabled = false;
                TopMost = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Minuty = TimeTable[comboBoxChooseTime.SelectedIndex];
            TimeSpan NewToShutDown = new TimeSpan(0, 0, Minuty, 0);
            SetTimer(NewToShutDown);
        }


        private void buttonCancel_Click(object sender, EventArgs e)
        {
            ResetTimer();
        }

        private void timerWarning_Tick(object sender, EventArgs e)
        {
            if (timer1.Enabled == false) // Musimy sie wylaczyc
            {
                timerWarning.Enabled = false;
            }
            else
            {
                if (ToShutDown.Milliseconds < 500) LabelLast.BackColor = Color.Red;
                else LabelLast.BackColor = Color.Yellow;
            }
        }

        private void comboBoxChooseTime_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string Text = comboBoxChooseTime.Text;
                Text = Text.Trim();
                int Pos = 0;
                string PureText = "";
                while (Pos < Text.Length)
                {
                    if (Text[Pos] >= '0' && Text[Pos] <= '9')
                    {
                        PureText = string.Concat(PureText, Text[Pos]);
                    }
                    else
                        break;
                    Pos++;
                }
                int TimeMin;
                bool OK = false;
                try
                {

                    TimeMin = Convert.ToInt32(PureText);
                    OK = true;
                }
                catch (System.FormatException)
                {
                    TimeMin = 0;
                }
                if (OK)
                {
                    TimeSpan ToShutDown = new TimeSpan(0, TimeMin, 0);
                    SetTimer(ToShutDown);
                }
            }
        }
    }
}

/*
  int a=EWX_FORCE;  //4
  int b=EWX_POWEROFF;  //8
  int c=EWX_REBOOT;   //2
  int d=EWX_SHUTDOWN; //  1
 
  BOOL ExitWindowsEx(

    UINT uFlags,	// shutdown operation
    DWORD dwReserved 	// reserved
   );
 
 EWX_FORCE=4	Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_LOGOFF	Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
EWX_POWEROFF=8	Shuts down the system and turns off the power. The system must support the power-off feature.Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.
EWX_REBOOT=2	Shuts down the system and then restarts the system. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.
EWX_SHUTDOWN=1	Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.

*/